package com.oz.ems;

import java.io.File;
import java.util.regex.Matcher;

import org.nutz.dao.Dao;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

import com.oz.ems.bean.Demo;

public class MainSetup implements Setup {
	private static final Log log = Logs.getLog(MainSetup.class);
	
	public String BASEPATH;

	@Override
	public void destroy(NutConfig nc) {
		// TODO Auto-generated method stub
	}

	@Override
	public void init(NutConfig nc) {
		// TODO Auto-generated method stub
		Ioc ioc = nc.getIoc();
		Dao dao = ioc.get(Dao.class);
		
		
		log.debug("............................................导入 dao..................................................");
//		log.debug(Json.toJson(ioc.getNames()));
		dao.create(Demo.class, false);
		
		String databasePathString = (new PropertiesProxy("custom/db.properties").get("db.url").replaceAll("database(.+)","database").replaceAll("jdbc:h2:file:", ""));
		File databasePath = new File(databasePathString.replaceAll("([/|(\\\\)]+)",Matcher.quoteReplacement(File.separator)));
		if(!databasePath.exists())
			databasePath.mkdir();
		File filePath = new File(databasePathString.replaceAll("database", "UPFILES"));
		if(!filePath.exists())
			filePath.mkdir();
		Utils.setFilesPath(filePath.getAbsolutePath());

		log.debug(".....................从数据库连接中 路径...赋值到.Utils.setBasePath..................."+ databasePathString );
		log.debug(".....................检查数据库存放文件夹，无则创建...................................."+ databasePath.getAbsolutePath() );
		log.debug(".....................检查数据库存放文件夹，无则创建...................................."+ filePath.getAbsolutePath() );
		
	}
}
